package com.ganmiao.server.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ganmiao.server.sys.dto.DistrictInfoDto;
import com.ganmiao.server.sys.entity.DistrictInfo;
import com.ganmiao.server.sys.repository.DistrictInfoRepository;
import com.ganmiao.server.sys.vo.DistrictInfoVo;
import com.ganmiao.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class DistrictInfoService {

    @Autowired
    private DistrictInfoRepository districtInfoRepository;

    public void insertDistrictInfo(List<DistrictInfoDto> dtoList) {
        if (CollectionUtils.isNotEmpty(dtoList)){
            for (DistrictInfoDto firstDistrictInfoDto : dtoList) {
                //第一层
                DistrictInfo firstDistrictInfo = new DistrictInfo();
                firstDistrictInfo.setRegionCode(firstDistrictInfoDto.getCode());
                firstDistrictInfo.setRegionName(firstDistrictInfoDto.getName());
                firstDistrictInfo.setParentRegionCode("0");
                firstDistrictInfo.setRegionLevel("1");
                districtInfoRepository.save(firstDistrictInfo);
                //第二层
                if (CollectionUtils.isNotEmpty(firstDistrictInfoDto.getChildren())){
                    for (DistrictInfoDto secondDistrictInfoDto : firstDistrictInfoDto.getChildren()) {
                        //第二层
                        DistrictInfo secondDistrictInfo = new DistrictInfo();
                        secondDistrictInfo.setRegionCode(secondDistrictInfoDto.getCode());
                        secondDistrictInfo.setRegionName(secondDistrictInfoDto.getName());
                        secondDistrictInfo.setParentRegionCode(firstDistrictInfoDto.getCode());
                        secondDistrictInfo.setRegionLevel("2");
                        districtInfoRepository.save(secondDistrictInfo);
                        //第三层
                        if (CollectionUtils.isNotEmpty(secondDistrictInfoDto.getChildren())){
                            for (DistrictInfoDto thirdDistrictInfoDto : secondDistrictInfoDto.getChildren()) {
                                DistrictInfo thirdDistrictInfo = new DistrictInfo();
                                thirdDistrictInfo.setRegionCode(thirdDistrictInfoDto.getCode());
                                thirdDistrictInfo.setRegionName(thirdDistrictInfoDto.getName());
                                thirdDistrictInfo.setParentRegionCode(secondDistrictInfoDto.getCode());
                                thirdDistrictInfo.setRegionLevel("3");
                                districtInfoRepository.save(thirdDistrictInfo);
                            }
                        }
                    }
                }



            }
        }






    }

    public List<DistrictInfoVo> getDistrictInfo() {
        LambdaQueryWrapper<DistrictInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DistrictInfo::getStatus, StatusUtils.PASS);
        wrapper.orderByAsc(DistrictInfo::getRegionCode);
        List<DistrictInfo> list = districtInfoRepository.list(wrapper);
        HashMap<String,DistrictInfoVo> firstMap = new LinkedHashMap<>();
        HashMap<String,DistrictInfoVo> secondMap = new HashMap<>();
        List<DistrictInfoVo> thirdList = new ArrayList<>();
        for (DistrictInfo districtInfo : list) {
            if (districtInfo.getRegionLevel().equals("1")){
                DistrictInfoVo vo = new DistrictInfoVo();
                vo.setCode(districtInfo.getRegionCode());
                vo.setName(districtInfo.getRegionName());
                vo.setParentCode("0");
                vo.setRegionLevel(districtInfo.getRegionLevel());
                List<DistrictInfoVo> voList = new ArrayList<>();
                vo.setChildren(voList);
                firstMap.put(districtInfo.getRegionCode(),vo);
            }else if (districtInfo.getRegionLevel().equals("2")){
                DistrictInfoVo vo = new DistrictInfoVo();
                vo.setCode(districtInfo.getRegionCode());
                vo.setName(districtInfo.getRegionName());
                vo.setParentCode(districtInfo.getParentRegionCode());
                vo.setRegionLevel(districtInfo.getRegionLevel());
                List<DistrictInfoVo> voList = new ArrayList<>();
                vo.setChildren(voList);
                secondMap.put(districtInfo.getRegionCode(),vo);
            }else {
                DistrictInfoVo vo = new DistrictInfoVo();
                vo.setCode(districtInfo.getRegionCode());
                vo.setName(districtInfo.getRegionName());
                vo.setParentCode(districtInfo.getParentRegionCode());
                vo.setRegionLevel(districtInfo.getRegionLevel());
                thirdList.add(vo);
            }
        }

        for (DistrictInfoVo vo : thirdList) {
            DistrictInfoVo secondVo =  secondMap.get(vo.getParentCode());
            secondVo.getChildren().add(vo);
        }

        secondMap.forEach((k,v)->{
            if (firstMap.containsKey(v.getParentCode())){
                DistrictInfoVo firstVo =  firstMap.get(v.getParentCode());
                firstVo.getChildren().add(v);
            }
        });

        List<DistrictInfoVo> resultList = new ArrayList<>();
        firstMap.forEach((k,v) ->{
            resultList.add(v);
        });

        return resultList;
        
    }

    public List<DistrictInfoVo> getSecondDistrictInfo() {
        LambdaQueryWrapper<DistrictInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DistrictInfo::getStatus, StatusUtils.PASS);
        wrapper.ne(DistrictInfo::getRegionLevel,"3");
        wrapper.orderByAsc(DistrictInfo::getRegionCode);
        List<DistrictInfo> list = districtInfoRepository.list(wrapper);
        HashMap<String,DistrictInfoVo> firstMap = new LinkedHashMap<>();
//        HashMap<String,DistrictInfoVo> secondMap = new HashMap<>();
        List<DistrictInfoVo> secondList = new ArrayList<>();
        for (DistrictInfo districtInfo : list) {
            if (districtInfo.getRegionLevel().equals("1")){
                DistrictInfoVo vo = new DistrictInfoVo();
                vo.setCode(districtInfo.getRegionCode());
                vo.setName(districtInfo.getRegionName());
                vo.setParentCode("0");
                vo.setRegionLevel(districtInfo.getRegionLevel());
                List<DistrictInfoVo> voList = new ArrayList<>();
                vo.setChildren(voList);
                firstMap.put(districtInfo.getRegionCode(),vo);
            }else {
                DistrictInfoVo vo = new DistrictInfoVo();
                vo.setCode(districtInfo.getRegionCode());
                vo.setName(districtInfo.getRegionName());
                vo.setParentCode(districtInfo.getParentRegionCode());
                vo.setRegionLevel(districtInfo.getRegionLevel());
                secondList.add(vo);
            }
        }

        for (DistrictInfoVo vo : secondList) {
            DistrictInfoVo secondVo = firstMap.get(vo.getParentCode());
            secondVo.getChildren().add(vo);
        }

        List<DistrictInfoVo> resultList = new ArrayList<>();
        firstMap.forEach((k,v) ->{
            resultList.add(v);
        });

        return resultList;

    }
}
